package com.zhang.modules.user.controller;


import com.zhang.common.web.respWrapper.Res;
import com.zhang.modules.api.annotation.ApiPermission;
import com.zhang.modules.user.entity.SysUser;
import com.zhang.modules.user.entity.cmd.LoginCmd;
import com.zhang.modules.user.entity.dto.SysUserDto;
import com.zhang.modules.user.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@Api(tags = "系统用户管理")
@RequestMapping("/api/sysUser")
@ApiPermission("sys:user:*")
public class SysUserController {

    private ISysUserService sysUserService;

    public SysUserController(ISysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @ApiOperation("系统用户列表")
    @ApiPermission("sys:user:list")
    @GetMapping("/page")
    public Res<Page<SysUserDto>> page(SysUser user,HttpServletRequest request){
        Page<SysUserDto> result = sysUserService.page(user, request.getParameterMap());
        return Res.ok(result);
    }


    @ApiOperation("添加系统用户")
    @ApiPermission("sys:user:add")
    @PostMapping("/add")
    public Res add(SysUser sysUser){
        sysUserService.save(sysUser);
        return Res.ok();
    }

    @ApiOperation("系统用户登录")
    @ApiPermission(anonymous = true)
    @PostMapping("/login")
    public Res<String> login(@Valid @RequestBody LoginCmd loginCmd){
        String token = sysUserService.login(loginCmd);
        return Res.ok("登录成功",token);
    }


}
